package leetcode_day._2022._202203._0110;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/3/1 19:41
 * Z 字形变换
 * 算法：模拟
 */
public class _01_6 {

    public String convert(String s, int numRows) {
        if (s.length() < numRows || numRows == 1) return s;
        List<StringBuilder> list = new ArrayList<>(numRows);
        for (int i = 0; i < numRows; i++) list.add(new StringBuilder());
        int x = 0, flag = 1;
        for (char c : s.toCharArray()) {
            list.get(x).append(c);
            if (x == 0) flag = 1;
            else if (x == numRows - 1) flag = -1;
            x += flag;
        }
        StringBuilder ans = new StringBuilder();
        for (StringBuilder sb : list) ans.append(sb);
        return ans.toString();
    }

}
